<?php

namespace Common\Model;

use Think\Model;

class ReportModel extends Model
{
    protected $_validate = array(
        array('uid', 'require', 'uid必须填写！', 1),
        array('objid', 'require', 'objid必须填写！', 1),
        array('content', 'require', '内容必须填写！', 1),
    );
    protected $_auto = array(
        array('audit', '0'),
        array('addtime', 'time', 1, 'function'),
    );

    /**
     * 举报类型对应的模型名称
     * @var array
     */
    protected $typemap = array(1 => 'Question', 2 => 'Answer', 3 => 'Bbs', 4 => 'BbsReply', 5 => 'Article');

    /**
     * 举报列表
     * @param      $map
     * @param      $page
     * @param      $sort
     * @param int  $limit
     * @param bool $showuser
     * @return mixed
     */
    public function get_list($map, $page, $sort, $limit = 10, $showuser = false)
    {
        if ($page) {
            $count = $this->where($map)->count();
            $Page = pager($count, $limit);
            $limit = $Page->firstRow . ',' . $Page->listRows;
            $page_str = $Page->fshow();
        }

        $list = $this->where($map)->order($sort)->limit($limit)->select();

        foreach ($list as $index => $item) {
            // 获取状态格式化输出字符串
            $audit = $item['audit'];
            $audit_case = array(
                '0'  => '<span style="color:#FF0000">未审核</span>',
                '1'  => '<span style="color:#009900">已审核</span>',
                '-1' => '<span style="color:#999999">未通过</span>'
            );
            $list[$index]['audit_str'] = $audit_case[$audit];

            // 获取举报类型格式化输出字符串
            $type = $item['type'];
            $type_case = array(
                '1' => '提问',
                '2' => '回答',
                '3' => '主题',
                '4' => '回帖',
                '5' => '文章'
            );
            $list[$index]['type_str'] = $type_case[$type];

            // 格式化举报时间
            $list[$index]['addtime_str'] = daterange($item['addtime']);

            // 获取举报人信息
            if ($showuser) {
                $list[$index]['user'] = D('User')->get_user_one($item['uid'], true, 1);
            }
        }

        $ret['list'] = $list;
        $ret['count'] = $count ?: count($list);
        $ret['page'] = $page_str;

        return $ret;
    }

    /**
     * 获取举报
     * @param $id
     */
    public function get_report_one($id)
    {
        $info = $this->find($id);

        // 格式化举报主体输出格式
        switch ($info['type']) {
            case '1':
                $q = D('Question')->get_question_one($info['objid']);
                $info['toobj'] = '<a href="' . $q['url'] . '" target="_blank">' . $q['title'] . '</a>';
                break;
            case '2':
                $answer = D('Answer')->get_answer_one($info['objid']);
                $q = D('Question')->get_question_one($answer['qid']);
                $info['toobj'] = '(' . $answer['content'] . ') <a href="' . $q['url'] . '" target="_blank">' . $q['title'] . '</a>';
                break;
            case '3':
                $bbs = D('Bbs')->get_bbs_one($info['objid']);
                $info['toobj'] = '<a href="' . $bbs['url'] . '" target="_blank">' . $bbs['content'] . '</a>';
                break;
            case '4':
                $reply = D('BbsReply')->get_reply_one($info['objid']);
                $bbs = D('Bbs')->get_bbs_one($reply['bid']);
                $info['toobj'] = '(' . $reply['content'] . ') <a href="' . $bbs['url'] . '" target="_blank">' . $bbs['content'] . '</a>';
                break;
            case '5':
                $article = D('Article')->get_one_id($info['objid']);
                $info['toobj'] = '<a href="' . $article['url'] . '" target="_blank">' . $article['title'] . '</a>';
                break;
        }

        return $info;
    }

    /**
     * 举报审核
     * @param int          $audit 审核后状态
     * @param array|string $ids   审核ID
     * @return bool|int
     */
    public function report_audit($audit, $ids)
    {
        return $this->where(array('id' => array('IN', $ids)))->save(compact('audit'));
    }

    /**
     * @param string $content 举报内容
     * @param int    $uid     举报人
     * @param int    $objid   举报对象ID
     * @param int    $type    举报类型
     */
    public function report_add($content, $objid, $type = 1)
    {
        # 用户是否登录
        $uid = session('ytata.uid');
        # 举报实体是否存在
        $obj = D($this->typemap[$type])->find($objid);
        if (empty($obj)) {
            $ret['status'] = 0;
            $ret['msg'] = '举报实体不存在';
            return $ret;
        }
        # 内容是否为空
        $data['content'] = $content;
        $data['objid'] = $objid;
        $data['type'] = $type;
        $data['uid'] = $uid;
        $data = $this->create($data);
        if (!$data) {
            $err = $this->getError();
            $ret['status'] = 0;
            $ret['msg'] = $this->getError();
            return $ret;
        }
        $newid = $this->add();

        # 用户举报相关操作
        // 完成举报任务
        D('Task')->exe_content_report($uid);

        $ret['status'] = 1;
        $ret['msg'] = '举报成功';
        return $ret;
    }

    // 删除举报[硬删除]
    public function report_del($ids)
    {
        return $this->delete($ids);
    }
}